<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :destroy-on-close="true" :visible="showDialog" :title="fromData.id?'编辑部门':'添加部门'" @close="btnCancel">
    <el-form ref="addDept" label-width="120px" :model="fromData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="fromData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="fromData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="fromData.manager" style="width:80%" placeholder="请选择" @blur="checkManager">
          <el-option
            v-for="item in empioyees"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="fromData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { departments, addDepartments, getDepartmentDetails, updatDepartment } from '@/api/departments'
import { getEmpioyeesList } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    nodeData: {
      type: Object,
      required: true
    }
  },
  data() {
    // 部门名字校验
    const checkRepeatName = async(rule, val, callback) => {
      const { depts } = await departments()
      let isRepect
      if (this.fromData.id) {
        // 编辑
        isRepect = depts.filter(item => item.pid === this.nodeData.pid).some(item => item.name === val && item.id !== this.nodeData.id)
      } else {
        // 新增
        isRepect = depts.filter(item => item.pid === this.nodeData.id).some(item => item.name === val)
      }
      isRepect ? callback(new Error('该部门已经存在！')) : callback()
    }
    // 部门编号校验
    const checkRepeatCode = async(rule, val, callback) => {
      const { depts } = await departments()
      let isRepect
      if (this.fromData.id) {
        // 编辑
        isRepect = depts.some(item => item.code === val && item.id !== this.nodeData.id)
      } else {
        // 新增
        isRepect = depts.some(item => item.code === val)
      }
      isRepect ? callback(new Error('部门编码重复，须唯一！')) : callback()
    }
    return {
      fromData: {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: checkRepeatCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      empioyees: []
    }
  },
  async created() {
    this.empioyees = await getEmpioyeesList()
  },
  methods: {
    // 校验部门负责人
    checkManager() {
      setTimeout(() => {
        this.$refs.addDept.validateField('manager')
      }, 100)
    },
    // 新增部门
    async  btnOk() {
      // 校验表单
      this.$refs.addDept.validate()
      if (this.fromData.id) {
        // 编辑
        await updatDepartment(this.fromData)
      } else {
        // 发送请求
        await addDepartments({ ...this.fromData, pid: this.nodeData.id })
      }
      // 提醒用户
      this.$message.success('操作成功')
      // 刷新数据
      this.$emit('addDepartments')
      // 关闭页面
      // this.$emit('closeDialog')
      this.$emit('update:showDialog', false)
    },
    btnCancel() {
      this.fromData = {
        code: '',
        introduce: '',
        manager: '',
        name: '',
        pid: ''
      }
      this.$refs.addDept.resetFields()
      this.$emit('update:showDialog', false)
    },
    async getDepartmentDetails(id) {
      this.fromData = await getDepartmentDetails(id)
    }
  }
}
</script>

<style>

</style>
